import 'package:backend.core/core/domain/entities/api/web/api_web_payment_method_failure.dart';
import 'package:backend.core/core/domain/entities/api/web/yookassa/api_yookassa_payment_method.dart';
import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_payment_method.dart';
import 'package:backend.purchases/domain/helpers/api/web/create_api_web_payment_method_failure_from_internal_one.dart';
import 'package:backend.purchases/domain/helpers/api/web/yookassa/create_api_yookassa_bank_card_info_from_internal_one.dart';

abstract class CreateApiYookassaPaymentMethodFromInternalOne {
  ApiYookassaPaymentMethod call({
    required YookassaPaymentMethod paymentMethod,
  });
}

class CreateApiYookassaPaymentMethodFromInternalOneImpl
    implements CreateApiYookassaPaymentMethodFromInternalOne {
  const CreateApiYookassaPaymentMethodFromInternalOneImpl({
    required CreateApiYookassaBankCardInfoFromInternalOne
        createApiYookassaBankCardInfoFromInternalOne,
    required CreateApiWebPaymentMethodFailureFromInternalOne
        createApiWebPaymentMethodFailureFromInternalOne,
  })  : _createApiYookassaBankCardInfoFromInternalOne =
            createApiYookassaBankCardInfoFromInternalOne,
        _createApiWebPaymentMethodFailureFromInternalOne =
            createApiWebPaymentMethodFailureFromInternalOne;

  final CreateApiYookassaBankCardInfoFromInternalOne _createApiYookassaBankCardInfoFromInternalOne;
  final CreateApiWebPaymentMethodFailureFromInternalOne
      _createApiWebPaymentMethodFailureFromInternalOne;

  @override
  ApiYookassaPaymentMethod call({
    required YookassaPaymentMethod paymentMethod,
  }) {
    final apiBankCardInfo = _createApiYookassaBankCardInfoFromInternalOne(
      bankCardInfo: paymentMethod.bankCardInfo,
    );

    ApiWebPaymentMethodFailure? apiFailure;

    if (paymentMethod.failure != null) {
      apiFailure = _createApiWebPaymentMethodFailureFromInternalOne(
        failure: paymentMethod.failure!,
      );
    }

    return ApiYookassaPaymentMethod(
      id: paymentMethod.id,
      userId: paymentMethod.userId,
      saved: paymentMethod.saved,
      test: paymentMethod.test,
      bankCardInfo: apiBankCardInfo,
      failure: apiFailure,
    );
  }
}
